package com.beiding.objectanalyzer;

import com.beiding.test.T;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OU2 {

/*
    private static void deepClone(Object a, Map<Integer, Object> mapping, List<KeyNode> all) {

        if (a == null) {
            return;
        }

        //一些不予处理的类型
        if (a instanceof Class) {
            return;
        }


        Object o = mapping.get(idOfObject(a));
        if (o == null) {

            if (a.getClass().isArray()) {
                cloneArray(a, mapping);
            } else {
                Object b = a;
                mapping.put(idOfObject(a), b);
                deepCopy(a, b, mapping, a.getClass());
            }

        }
    }


    //深度复制
    private static void deepCopy(Object a, Object b, Map<Integer, Object> mapping, Class t) {

        Field[] declaredFields = t.getDeclaredFields();
        for (Field declaredField : declaredFields) {

            *//*
                通常枚举集都是final类型的
             *//*
            try {
                if (!Modifier.isStatic(declaredField.getModifiers())) {
                    declaredField.setAccessible(true);
                    if (Modifier.isFinal(declaredField.getModifiers())) {
                        declaredField.set(b, declaredField.get(a));
                    } else {
                        Class<?> type = declaredField.getType();
                        if (Object.class.isAssignableFrom(type)) {
                            declaredField.set(b, deepClone(declaredField.get(a), mapping));
                        } else {//基本数据类型
                            declaredField.set(b, declaredField.get(a));
                        }

                        declaredField.set(b, declaredField.get(a));
                    }
                }
            } catch (Exception ignore) {

            }
        }

        t = t.getSuperclass();
        if (t != Object.class) {
            deepCopy(a, b, mapping, t);//深度复制
        }


    }

    private static Object cloneArray(Object arr, Map<Integer, Object> mapping) {

        if (arr == null) {
            return null;
        }
        Class<?> type = arr.getClass();
        Class<?> componentType = type.getComponentType();


        //判断是否是对象类型
        if (Object.class.isAssignableFrom(componentType)) {
            Object[] objects = (Object[]) arr;
            Object o = Array.newInstance(componentType, objects.length);


            //建立映射
            mapping.put(idOfObject(arr), o);
            for (int i = 0; i < objects.length; i++) {
                final Object tv = objects[i];
                Array.set(o, i, deepClone(tv, mapping));
            }
            return o;
        } else {//如果不是,那么就是基本数据类型
            int length = Array.getLength(arr);
            Object o = Array.newInstance(componentType, length);
            mapping.put(idOfObject(arr), o);
            System.arraycopy(arr, 0, o, 0, length);
            return o;
        }

    }


    private static int idOfObject(Object obj) {
        return System.identityHashCode(obj);
    }


    public static List<KeyNode> deepClone(Object a) {
        if (a == null) {
            return null;
        }


        //使用对象在内存中的偏移量作为key
        HashMap<Integer, Object> objectObjectHashMap = new HashMap<>();
        List<KeyNode> all = new ArrayList<>();
        deepClone(a, objectObjectHashMap, all);
        return all;
    }*/

}
